#include "tjc_screen.hpp"

uart_v2 *screen_uart = nullptr;
tjc_screen *screen = nullptr;

void screen_test_thread()
{
    while (!screen)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    int i = 0;
    uint8_t pic_id = 3;
    while (1)
    {
        screen->set_status_item(tjc_screen::tjc_screen_status_rs485_12,
                                std::to_string(i),
                                (tjc_screen::tjc_screen_pic)pic_id);
        screen->set_status_item(tjc_screen::tjc_screen_status_rs485_34,
                                std::to_string(i),
                                (tjc_screen::tjc_screen_pic)pic_id);
        screen->set_status_item(tjc_screen::tjc_screen_status_rs485_5678,
                                std::to_string(i),
                                (tjc_screen::tjc_screen_pic)pic_id);
        screen->set_status_item(tjc_screen::tjc_screen_status_i2c,
                                std::to_string(i),
                                (tjc_screen::tjc_screen_pic)pic_id);
        screen->set_status_item(tjc_screen::tjc_screen_status_spi,
                                std::to_string(i),
                                (tjc_screen::tjc_screen_pic)pic_id);
        screen->set_status_item(tjc_screen::tjc_screen_status_usb,
                                std::to_string(i),
                                (tjc_screen::tjc_screen_pic)pic_id);
        screen->set_status_item(tjc_screen::tjc_screen_status_next_page,
                                std::to_string(i),
                                (tjc_screen::tjc_screen_pic)(pic_id - 3));

        screen->set_status_item(tjc_screen::tjc_screen_number,
                                std::to_string(i),
                                (tjc_screen::tjc_screen_pic)(pic_id - 3));
        i++;
        pic_id++;
        if (pic_id > 7)
            pic_id = 3;

        std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    }
}

void screen_test_fresh_thread()
{
    screen_uart = new uart_v2("/dev/ttyS4", 115200, 8, 1);
    screen = new tjc_screen(screen_uart);

    if (!screen || !screen_uart)
    {
        if (screen)
            delete screen;
        if (screen_uart)
            delete screen_uart;
    }

    tjc_screen::tjc_screen_page page1;
    uint8_t bt_val = 0;

    while (1)
    {
        if(screen->read_page(page1) != YJ_OK)
        {
            
            std::this_thread::sleep_for(std::chrono::milliseconds(500));
            continue;
        }
        LOG(INFO) << "page1:" << (int)page1;
        switch (page1)
        {
        case tjc_screen::tjc_screen_page_status:
            screen->fresh_status_screen();
            break;
        case tjc_screen::tjc_screen_page_button:
            screen->get_value("start_bt", "val", bt_val);
            if (bt_val == 1)
            {
                screen->change_page(tjc_screen::tjc_screen_page_number);
            }
            break;
        case tjc_screen::tjc_screen_page_number:
            screen->fresh_number_screen();
            break;
        default:
            break;
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
}

int main()
{
    std::thread s_t(screen_test_thread);
    std::thread f_t(screen_test_fresh_thread);

    while (1)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }

    return 0;
}